package com.ray.service;

import com.alibaba.fastjson.JSONObject;
import com.ray.bean.*;
import com.ray.exception.ServiceException;
import com.ray.mapper.DormManageMapper;
import com.ray.mapper.EnergyMapper;
import com.ray.pojo.DormEWTotalPojo;
import com.ray.pojo.PeoplesFeeForDormPojo;
import com.ray.pojo.UserRecordsPojo;
import com.ray.protocol.constants.TimeConst;
import com.ray.service.model.dorm.DormStatisticsOut;
import com.ray.service.model.dorm.PersonOfDormStatisticsOut;
import com.ray.util.StringUtil;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 宿舍管理服务层
 * <p>Title: DormManageService</p>
 * <p>Description: </p>
 *
 * @author 李斌
 * @date 2018年4月26日
 */
@Service
public class DormManageService {

    @Autowired
    EnergyMapper energyMapper;
    @Autowired
    private DormManageMapper dormManageMapper;

    /**
     * @return boolean
     * @throws ServiceException 新增宿舍管理信息
     * @date 2018年4月26日 上午8:44:38
     * @author 李斌
     */
    public boolean addDormManage(Map<String, Object> map) throws ServiceException {
        boolean flag = false;
        String createTime = new DateTime().toString(TimeConst.SECOND_FORMAT_WITH_SEPARATOR);
        DormManageBean dormManageBean = JSONObject.parseObject(map.get("data").toString(), DormManageBean.class);
        dormManageBean.setCompany(map.get("loginCompany").toString());
        dormManageBean.setCreateUser(map.get("loginUserId").toString());
        dormManageBean.setCreateTime(createTime);
        String id = StringUtil.getUUID();
        dormManageBean.setId(id);
        //查重宿舍编号
        map.put("dormNo", dormManageBean.getDormNo());
        map.put("id", dormManageBean.getId());
        List<DormManageBean> dormManageBeans = this.getDormByDormNo(map);
        if (dormManageBeans != null && dormManageBeans.size() > 0) {
            throw new ServiceException("宿舍编号重复,请重新输入!");
        }
        int dormRow = dormManageMapper.addDormManage(dormManageBean);
        DormManageToEnergyBean dormManageToEnergyAndWater = new DormManageToEnergyBean();
        dormManageToEnergyAndWater.setDormId(dormManageBean.getId());
        Map energyParamMap = new HashMap();
        List<DormManageToEnergyRecordsBean> dormManageToEnergyRecordsBeans = new ArrayList<>();
        if (dormManageBean.getWater() != null && !("".equals(dormManageBean.getWater().getId()))) {
            //判断当前选中的水电表是否被使用
            map.put("id", dormManageBean.getWater().getId());
            map.put("state", "1");
            EnergyBean water = energyMapper.getEnergy(map);
            if (water != null) {
                throw new ServiceException("水表已经被占用");
            }
            dormManageToEnergyAndWater.setWaterId(dormManageBean.getWater().getId());
            energyParamMap.put("id", dormManageToEnergyAndWater.getWaterId());
            energyParamMap.put("state", 1);
            energyMapper.updateState(energyParamMap);
            DormManageToEnergyRecordsBean dormManageToEnergyRecordsBean = new DormManageToEnergyRecordsBean();
            dormManageToEnergyRecordsBean.setDormId(dormManageBean.getId());
            dormManageToEnergyRecordsBean.setEnergyId(dormManageBean.getWater().getId());
            dormManageToEnergyRecordsBean.setCreateTime(createTime);
            //1是水表,2是电表
            dormManageToEnergyRecordsBean.setType("1");
            dormManageToEnergyRecordsBean.setState("1");
            dormManageToEnergyRecordsBeans.add(dormManageToEnergyRecordsBean);
        }
        if (dormManageBean.getEnergy() != null && !("".equals(dormManageBean.getEnergy().getId()))) {
            map.put("id", dormManageBean.getEnergy().getId());
            map.put("state", "1");
            EnergyBean water = energyMapper.getEnergy(map);
            if (water != null) {
                throw new ServiceException("电表已经被占用");
            }
            dormManageToEnergyAndWater.setEnergyId(dormManageBean.getEnergy().getId());
            energyParamMap.put("id", dormManageToEnergyAndWater.getEnergyId());
            energyParamMap.put("state", 1);
            energyMapper.updateState(energyParamMap);
            DormManageToEnergyRecordsBean dormManageToEnergyRecordsBean = new DormManageToEnergyRecordsBean();
            dormManageToEnergyRecordsBean.setDormId(dormManageBean.getId());
            dormManageToEnergyRecordsBean.setEnergyId(dormManageBean.getEnergy().getId());
            dormManageToEnergyRecordsBean.setCreateTime(createTime);
            //1是水表,2是电表
            dormManageToEnergyRecordsBean.setType("2");
            dormManageToEnergyRecordsBean.setState("1");
            dormManageToEnergyRecordsBeans.add(dormManageToEnergyRecordsBean);
        }
        if (dormManageToEnergyAndWater.getEnergyId() != null || dormManageToEnergyAndWater.getWaterId() != null) {
            dormManageMapper.addDormManageToEnergyAndWater(dormManageToEnergyAndWater);
            map.put("energyRecords", dormManageToEnergyRecordsBeans);
            dormManageMapper.addDormMangeToEnergyRecords(map);
        }

        if (dormManageBean.getUserList() != null && dormManageBean.getUserList().size() > 0) {
            Map<String, Object> paramMap = new HashMap<>();
            List<UserBean> userList = dormManageBean.getUserList();
            List<DormManageToUserRecordsBean> dormManageToUserRecordsBeans = new ArrayList<>();
            List<DormManageToUserBean> dormManageToUserBeans = new ArrayList<>();
            List<String> ids = new ArrayList<>();
            for (UserBean user : userList) {
                DormManageToUserBean dormManageToUserBean = new DormManageToUserBean();
                dormManageToUserBean.setDormId(dormManageBean.getId());
                dormManageToUserBean.setUserId(user.getId());
                dormManageToUserBeans.add(dormManageToUserBean);
                DormManageToUserRecordsBean dormManageToUserRecordsBean = new DormManageToUserRecordsBean();
                dormManageToUserRecordsBean.setDormId(dormManageBean.getId());
                dormManageToUserRecordsBean.setUserId(user.getId());
                dormManageToUserRecordsBean.setState("1");
                dormManageToUserRecordsBean.setCreateTime(createTime);
                dormManageToUserRecordsBeans.add(dormManageToUserRecordsBean);
                ids.add(user.getId());
            }
            List<DormManageToUserBean> dormToUserList = dormManageMapper.getUserOnNotInDorm(ids);
            asertUserInOtherDorm(dormToUserList);
            paramMap.put("userList", dormManageToUserBeans);
            int userRow = dormManageMapper.addDormManageToUser(paramMap);
            if (userRow <= 0) {
                throw new ServiceException("宿舍新增人员出错");
            }

            paramMap.put("userRecordsList", dormManageToUserRecordsBeans);
            dormManageMapper.addDormManageToUserRecords(paramMap);
        }
        if (dormRow > 0) {
            flag = true;
        }
        return flag;
    }

    private void asertUserInOtherDorm(List<DormManageToUserBean> dormToUserList) {
        if (dormToUserList != null && dormToUserList.size() > 0) {
            String exceptionText = "添加的人员中,有其他宿舍在住人员：如下\n";
            StringBuffer sb = new StringBuffer();
            for (DormManageToUserBean user : dormToUserList) {
                sb.append("人员: " + user.getUserId() + "\n");
            }
            throw new ServiceException(exceptionText + sb.toString());
        }
    }

    /**
     * 根据条件查询宿舍信息
     *
     * @return void
     * @throws
     * @date 2018年4月26日 下午4:12:06
     * @author 李斌
     */
    public List<DormManageBean> getDormManageByDetail(Map<String, Object> map) {
        return dormManageMapper.getDormManageByDetail(map);
    }

    /**
     * 获取水电信息
     *
     * @return List<EnergyBean>
     * @throws
     * @date 2018年4月26日 下午5:32:13
     * @author 李斌
     */
    public List<EnergyBean> getEnergyAndWaterByDetail(Map<String, Object> map) {
        return dormManageMapper.getEnergyAndWaterByDetail(map);
    }

    /**
     * @return boolean
     * @throws ServiceException 修改宿舍信息
     * @date 2018年4月26日 下午7:18:20
     * @author 李斌
     */
    public void updateDormManageByDetail(Map<String, Object> map) throws ServiceException {
        String createTime = new DateTime().toString(TimeConst.SECOND_FORMAT_WITH_SEPARATOR);
        DormManageBean dormManage = JSONObject.parseObject(map.get("data").toString(), DormManageBean.class);
        map.put("id", dormManage.getId());
        DormManageBean oldDormManage = null;
        List<DormManageBean> oldDormManages = this.getDormManageByDetail(map);
        if (oldDormManages != null && oldDormManages.size() > 0) {
            oldDormManage = oldDormManages.get(0);
        }
        dormManage.setLastModifier(map.get("loginUserId").toString());
        dormManage.setLastModifyTime(createTime);
        map.put("dormNo", dormManage.getDormNo());
        map.put("id", dormManage.getId());
        List<DormManageBean> dormManageBeans = this.getDormByDormNo(map);
        if (dormManageBeans != null && dormManageBeans.size() > 0) {
            throw new ServiceException("宿舍编号重复,请重新输入!");
        }
        DormManageToEnergyBean energyBean = dormManageMapper.getEnergyAndWaterByDormId(dormManage.getId());
        DormManageToEnergyBean dormManageToEnergyBean = new DormManageToEnergyBean();
        if (oldDormManage != null) {
            if (dormManage.getWater() != null && !("".equals(dormManage.getWater().getId()))) {
                if (oldDormManage.getWater() != null && (!oldDormManage.getWater().getId().equals(dormManage.getWater().getId()))) {
                    map.put("id", oldDormManage.getWater().getId());
                    map.put("state", 2);
                    energyMapper.updateState(map);
                    DormManageToEnergyRecordsBean dterb = new DormManageToEnergyRecordsBean();
                    dterb.setRemoveTime(createTime);
                    dterb.setEnergyId(oldDormManage.getWater().getId());
                    dterb.setState("0");
                    dormManageMapper.updateDormManageToEnergyAndWaterRecords(dterb);
                }
            } else {
                if (oldDormManage.getWater() != null) {
                    map.put("id", oldDormManage.getWater().getId());
                    map.put("state", 2);
                    DormManageToEnergyRecordsBean dterb = new DormManageToEnergyRecordsBean();
                    dterb.setRemoveTime(createTime);
                    dterb.setEnergyId(oldDormManage.getWater().getId());
                    dterb.setState("0");
                    dormManageMapper.updateDormManageToEnergyAndWaterRecords(dterb);
                    energyMapper.updateState(map);
                    energyBean.setWaterId(null);
                    dormManageMapper.updateDormManageToEnergy(energyBean);
                }
            }
            if (dormManage.getEnergy() != null && !("".equals(dormManage.getEnergy().getId()))) {
                if (oldDormManage.getEnergy() != null && (!oldDormManage.getEnergy().getId().equals(dormManage.getEnergy().getId()))) {
                    map.put("id", oldDormManage.getEnergy().getId());
                    map.put("state", 2);
                    energyMapper.updateState(map);
                    DormManageToEnergyRecordsBean dterb = new DormManageToEnergyRecordsBean();
                    dterb.setDormId(oldDormManage.getId());
                    dterb.setRemoveTime(createTime);
                    dterb.setEnergyId(oldDormManage.getEnergy().getId());
                    dterb.setState("0");
                    dormManageMapper.updateDormManageToEnergyAndWaterRecords(dterb);
                }
            } else {
                if (oldDormManage.getEnergy() != null) {
                    map.put("id", oldDormManage.getEnergy().getId());
                    map.put("state", 2);
                    energyMapper.updateState(map);
                    DormManageToEnergyRecordsBean dterb = new DormManageToEnergyRecordsBean();
                    dterb.setDormId(oldDormManage.getId());
                    dterb.setRemoveTime(createTime);
                    dterb.setEnergyId(oldDormManage.getEnergy().getId());
                    dterb.setState("0");
                    dormManageMapper.updateDormManageToEnergyAndWaterRecords(dterb);
                    energyBean.setEnergyId(null);
                    dormManageMapper.updateDormManageToEnergy(energyBean);
                }
            }
        }
        if (dormManage.getWater() != null && !("".equals(dormManage.getWater().getId()))) {
            map.put("id", dormManage.getWater().getId());
            map.put("state", "1");
            EnergyBean water = energyMapper.getEnergy(map);
            if (water != null) {
                if (energyBean != null && energyBean.getWaterId() != null && !water.getId().equals(energyBean.getWaterId())) {
                    throw new ServiceException("水表已经被占用");
                }
            }
            energyMapper.updateState(map);
            dormManageToEnergyBean.setWaterId(dormManage.getWater().getId());
            DormManageToEnergyRecordsBean dterb = new DormManageToEnergyRecordsBean();
            //判断以前有没有电表或水表
            if (oldDormManage != null && oldDormManage.getWater() != null) {
                //以前水表是否和现在水表是否不相同
                if (!oldDormManage.getWater().getId().equals(dormManage.getWater().getId())) {
                    dterb.setDormId(dormManage.getId());
                    dterb.setEnergyId(dormManage.getWater().getId());
                    dterb.setCreateTime(createTime);
                    dterb.setType("1");//1是水表,2是电表
                    dterb.setState("1");
                    dormManageMapper.addDormManageToEnergyAndWaterRecords(dterb);
                }
            } else {//以前没有电表或水表
                dterb.setDormId(dormManage.getId());
                dterb.setEnergyId(dormManage.getWater().getId());
                dterb.setCreateTime(createTime);
                dterb.setType("1");//1是水表,2是电表
                dterb.setState("1");
                dormManageMapper.addDormManageToEnergyAndWaterRecords(dterb);
            }
        }
        if (dormManage.getEnergy() != null && !("".equals(dormManage.getEnergy().getId()))) {
            map.put("id", dormManage.getEnergy().getId());
            map.put("state", "1");
            EnergyBean energy = energyMapper.getEnergy(map);
            if (energy != null) {
                if (energyBean != null && energyBean.getEnergyId() != null && !energy.getId().equals(energyBean.getEnergyId())) {
                    throw new ServiceException("电表已经被占用");
                }
            }
            energyMapper.updateState(map);
            dormManageToEnergyBean.setEnergyId(dormManage.getEnergy().getId());
            DormManageToEnergyRecordsBean dterb = new DormManageToEnergyRecordsBean();
            //判断以前是否有电表
            if (oldDormManage != null && oldDormManage.getEnergy() != null) {
                //以前水表是否和现在水表是否不相同
                if (!oldDormManage.getEnergy().getId().equals(dormManage.getEnergy().getId())) {
                    dterb.setDormId(dormManage.getId());
                    dterb.setEnergyId(dormManage.getEnergy().getId());
                    dterb.setCreateTime(createTime);
                    dterb.setType("2");//1是水表,2是电表
                    dterb.setState("1");
                    dormManageMapper.addDormManageToEnergyAndWaterRecords(dterb);
                }
            } else {//以前没有电表或水表
                dterb.setDormId(dormManage.getId());
                dterb.setEnergyId(dormManage.getEnergy().getId());
                dterb.setCreateTime(createTime);
                dterb.setType("2");//1是水表,2是电表
                dterb.setState("1");
                dormManageMapper.addDormManageToEnergyAndWaterRecords(dterb);
            }

        }
        if (dormManageToEnergyBean.getWaterId() != null || dormManageToEnergyBean.getEnergyId() != null) {
            dormManageToEnergyBean.setDormId(dormManage.getId());
            DormManageToEnergyBean oldDomManageToEnergyAndWater = dormManageMapper.getEnergyAndWaterByDormId(dormManage.getId());
            if (oldDomManageToEnergyAndWater != null) {
                dormManageMapper.updateDormManageToEnergy(dormManageToEnergyBean);
            } else {
                dormManageMapper.addDormManageToEnergyAndWater(dormManageToEnergyBean);
            }
        }
        /**
         * 修改宿舍人员方式重构
         * 通过list获取差集,根据当前用户,判断删除或新增
         */
        Map<String, Object> dormParam = new HashMap<>();
        //修改的宿舍人员与原宿舍人员的差集(需要新增的宿舍人员)
        List<String> addUsers;
        //原宿舍人员与修改宿舍人员的差集(需要删除的宿舍人员)
        List<String> deleteUsers;
        dormParam.put("loginCompany", map.get("loginCompany"));
        dormParam.put("dormId", dormManage.getId());
        //获取当前宿舍人员
        List<String> userIds = dormManageMapper.getDormManageToUserByDormId(dormParam);
        //需要新增的人员数
        List<String> ids = new ArrayList<>();
        if (userIds != null && userIds.size() > 0) {
            //删除宿舍所有人员
            dormManageMapper.deleteDormManageToUser(dormManage.getId());
        }
        //新增宿舍人员
        if (dormManage.getUserList() != null && dormManage.getUserList().size() > 0) {
            Map<String, Object> paramMap = new HashMap<>();
            List<DormManageToUserBean> userList = new ArrayList<>();
            List<DormManageToUserRecordsBean> userRecordsList = new ArrayList<>();

            for (UserBean user : dormManage.getUserList()) {
                ids.add(user.getId());
                DormManageToUserBean dormManageToUserBean = new DormManageToUserBean();
                dormManageToUserBean.setDormId(dormManage.getId());
                dormManageToUserBean.setUserId(user.getId());
                userList.add(dormManageToUserBean);
            }
            //判断是否有其他宿舍的人员,以免人员重复添加
            addUsers = com.ray.util.ArrayUtils.subtract(ids, userIds);
            if (addUsers != null && addUsers.size() > 0) {
                List<DormManageToUserBean> dormToUserList = dormManageMapper.getUserOnNotInDorm(addUsers);
                asertUserInOtherDorm(dormToUserList);
            }
            //添加宿舍人员对应表信息
            paramMap.put("userList", userList);
            dormManageMapper.addDormManageToUser(paramMap);

            /**
             * 处理宿舍人员历史记录表
             */
            //添加宿舍人员历史记录表
            for (String userId : addUsers) {
                DormManageToUserRecordsBean dormManageToUserRecordsBean = new DormManageToUserRecordsBean();
                dormManageToUserRecordsBean.setCreateTime(createTime);
                dormManageToUserRecordsBean.setDormId(dormManage.getId());
                dormManageToUserRecordsBean.setUserId(userId);
                dormManageToUserRecordsBean.setState("1");
                userRecordsList.add(dormManageToUserRecordsBean);
            }
            if (userRecordsList.size() > 0) {
                paramMap.put("userRecordsList", userRecordsList);
                int userRow = dormManageMapper.addDormManageToUserRecords(paramMap);
                if (userRow <= 0) {
                    throw new ServiceException("修改宿舍人员出错");
                }
            }
        }
        //需要删除的宿舍人员
        deleteUsers = com.ray.util.ArrayUtils.subtract(userIds, ids);
        //修改搬离的人员
        if (deleteUsers != null && deleteUsers.size() > 0) {
            map.put("deleteUsers", deleteUsers);
            map.put("state", "0");
            map.put("removeTime", createTime);
            //设置用户的搬离状态
            dormManageMapper.updateDormMangeToUserRecords(map);
            map.remove("deleteUsers");
            map.remove("state");
        }
        dormManageMapper.updateDormManageDetail(dormManage);
    }

    /**
     * 查询所有用户
     *
     * @return List<UserBean>
     * @throws
     * @date 2018年4月27日 上午9:31:15
     * @author 李斌
     */
    public List<UserBean> getAllUserOnIdAndName(Map<String, Object> map) {
        //公司所有人员
        List<UserBean> userBeans = dormManageMapper.getAllUserOnIdAndName(map);
        //公司宿舍人员
        List<String> ids = dormManageMapper.getAllUserOnIdAndNameToDorm(map);
        for (UserBean user : userBeans) {
            if (ids.contains(user.getId())) {
                user.setStatus("1");
            } else {
                user.setStatus("0");
            }
        }
        return userBeans;
    }

    /**
     * 查询宿舍编号(查重)
     *
     * @return Object
     * @throws
     * @date 2018年4月27日 上午9:31:26
     * @author 李斌
     */
    public List<DormManageBean> getDormByDormNo(Map<String, Object> map) {
        return dormManageMapper.getDormByDormNo(map);
    }

    /**
     * 分页宿舍信息
     *
     * @return void
     * @throws
     * @date 2018年4月28日 下午2:05:04
     * @author 李斌
     */
    public DatatablesViewPageBean<DormManageBean> getDormMangeByPage(Map<String, Object> map) {
        Integer currentPage = Integer.valueOf(map.get("currentPage").toString());
        Integer pageSize = Integer.valueOf(map.get("pageSize").toString());
        Integer start = (currentPage - 1) * pageSize;
        map.put("start", start);
        map.put("length", pageSize);
        DatatablesViewPageBean<DormManageBean> datatablesViewPageBean = new DatatablesViewPageBean<DormManageBean>();
        List<DormManageBean> dormManageList = dormManageMapper.getDormMangeByPage(map);
        int count = dormManageMapper.getDormMangeByPageCount(map);
        datatablesViewPageBean.setiTotalRecords(count);
        datatablesViewPageBean.setReturnData(dormManageList);
        return datatablesViewPageBean;

    }

    /**
     * 禁用宿舍
     *
     * @return void
     * @throws
     * @date 2018年4月30日 下午2:36:44
     * @author 李斌
     */
    public void setDormToDisable(Map<String, Object> map) {
        if (map.get("dormId") != null) {
            String dormId = map.get("dormId").toString();
            DormManageToEnergyBean dormManageEW = dormManageMapper.getEnergyAndWaterByDormId(dormId);
            if (dormManageEW != null) {
                if (dormManageEW.getWaterId() != null) {
                    map.put("id", dormManageEW.getWaterId());
                    map.put("state", 2);
                    energyMapper.updateState(map);
                }
                if (dormManageEW.getEnergyId() != null) {
                    map.put("id", dormManageEW.getEnergyId());
                    map.put("state", 2);
                    energyMapper.updateState(map);
                }
                //删除宿舍水电
                dormManageMapper.deleteDormWaterAndEnergyRecordsByDormId(map);
                dormManageMapper.deleteDormWaterAndEnergyByDormId(map);
            }
            //查询宿舍是否有人员
            Map<String, Object> dormParam = new HashMap<>();
            dormParam.put("loginCompany", map.get("loginCompany"));
            dormParam.put("dormId", dormId);
            List<String> dormPeoples = dormManageMapper.getDormManageToUserByDormId(dormParam);
            if (dormPeoples != null && dormPeoples.size() > 0) {
                //删除宿舍人员
                dormManageMapper.deleteDormManageToUserRecordsByDormId(dormId);
                dormManageMapper.deleteDormManageToUser(dormId);
            }
            //删除宿舍
            dormManageMapper.setDormToDisable(map);
        }
    }

    /**
     * @return Object
     * @throws
     * @date 2018年4月29日 下午4:36:27
     * @author 李斌
     */
    public List<DormEWTotalPojo> getDormEWTotalData(Map<String, Object> map) {
        /**
         * 获取总数据,然后汇总
         */
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
        if (map.get("startTime") != null) {
            String startTime = simpleDateFormat.format(new Date(Long.parseLong(map.get("startTime").toString())));
            map.put("startTime", startTime);
        }
        if (map.get("endTime") != null) {
            simpleDateFormat.format(new Date(Long.parseLong(map.get("endTime").toString())));
            String endTime = simpleDateFormat.format(new Date(Long.parseLong(map.get("endTime").toString())));
            map.put("endTime", endTime);
        }
        //获取过去使用的水电表
        List<String> energyIds = dormManageMapper.getDormToEnergyRecordsOnEWId(map);
        map.put("energyIds", energyIds);
        //查询宿舍水电费
        List<DormEWTotalPojo> dormEWTotalPojos = dormManageMapper.getDormEWTotalData(map);
        List<DormEWTotalPojo> resultData = new ArrayList<>();
        List<Double> waterList = new ArrayList<>();
        List<Double> energyList = new ArrayList<>();
        List<Double> dateList = new ArrayList<>();
        Map<String, List<DormEWTotalPojo>> hashMap = new HashMap<>();
        String dormId;
        List<DormEWTotalPojo> waterAndEnergyList;
        //分组宿舍水电费,根据宿舍Id进行分组
        for (DormEWTotalPojo dormEWTotalPojo : dormEWTotalPojos) {
            dormId = dormEWTotalPojo.getDormId();
            waterAndEnergyList = hashMap.get(dormId);
            if (waterAndEnergyList == null || waterAndEnergyList.size() < 1) {
                waterAndEnergyList = new ArrayList<>();
                waterAndEnergyList.add(dormEWTotalPojo);
                hashMap.put(dormId, waterAndEnergyList);
            } else {
                waterAndEnergyList.add(dormEWTotalPojo);
                hashMap.put(dormId, waterAndEnergyList);
            }
        }
        //遍历所有宿舍水电记录
        //获取时间段内宿舍人员记录表
        List<DormManageToUserRecordsBean> userRecordsBeans = dormManageMapper.getDormManageToUserRecords(map);
        //汇总宿舍水电费用信息
        for (Map.Entry<String, List<DormEWTotalPojo>> entry : hashMap.entrySet()) {
            Double sumWater = 0.0;
            Double sumEnergy = 0.0;
            //汇总宿舍水电费
            for (DormEWTotalPojo pojo : entry.getValue()) {
                sumWater += pojo.getWaterOnDay();
                sumEnergy += pojo.getEnergyOnDay();
                waterList.add(pojo.getWaterOnDay());
                energyList.add(pojo.getEnergyOnDay());
                dateList.add(Double.valueOf(pojo.getDate() != null ? pojo.getDate() : "0.0"));
            }
            DormEWTotalPojo WaterAndEnergy = new DormEWTotalPojo();
            WaterAndEnergy.setSumEnergy(sumEnergy);
            WaterAndEnergy.setSumWater(sumWater);
            WaterAndEnergy.setMaxEnergy(Collections.max(energyList));
            WaterAndEnergy.setMaxWater(Collections.max(waterList));
            WaterAndEnergy.setMinEnergy(Collections.min(energyList));
            WaterAndEnergy.setMinWater(Collections.min(waterList));
            WaterAndEnergy.setDate((Collections.max(dateList) - Collections.min(dateList)) + "");
            Map<String, Object> dormParam = new HashMap<>();
            dormParam.put("dormId", entry.getKey());
            dormParam.put("loginCompany", map.get("loginCompany"));
            WaterAndEnergy.setDormNo(dormManageMapper.getDormNoById(dormParam));
            //初始化宿舍人员数目
            int peoples = 0;
            //根据宿舍id获取宿舍人员数
            for (DormManageToUserRecordsBean userRecordsBean : userRecordsBeans) {
                if (entry.getKey().equals(userRecordsBean.getDormId())) {
                    peoples++;
                }
            }
            WaterAndEnergy.setPeoples(peoples);
            if (peoples > 1) {
                WaterAndEnergy.setAvgMoney((WaterAndEnergy.getSumEnergy() + WaterAndEnergy.getSumWater()) / peoples);
                resultData.add(WaterAndEnergy);
            }
        }
        return resultData;
    }

    /**
     * @return List<PeoplesFeeForDormPojo>
     * @throws
     * @date 2018年5月1日 下午4:25:56
     * @author 李斌
     */
    public List<PeoplesFeeForDormPojo> getPeoplesFeeForDorm(Map<String, Object> map) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
        if (map.get("startTime") != null) {
            String startTime = simpleDateFormat.format(new Date(Long.parseLong(map.get("startTime").toString())));
            map.put("startTime", startTime);
        }
        if (map.get("endTime") != null) {
            simpleDateFormat.format(new Date(Long.parseLong(map.get("endTime").toString())));
            String endTime = simpleDateFormat.format(new Date(Long.parseLong(map.get("endTime").toString())));
            map.put("endTime", endTime);
        }
        List<PeoplesFeeForDormPojo> peoplesFeeForDormPojos = new ArrayList<>();
        //查询宿舍水电表Id通过宿舍水电记录明细表获取
        List<String> energyIds = dormManageMapper.getDormToEnergyRecordsOnEWId(map);
        map.put("energyIds", energyIds);
        List<DormEWTotalPojo> dormEWTotalPojos = dormManageMapper.getPeoplesFeeForDorm(map);
        Map<String, List<DormEWTotalPojo>> hashMap = new HashMap<>();
        String dormId;
        List<DormEWTotalPojo> waterAndEnergyList;
        //将宿舍水电费用宿舍Id进行分组
        for (DormEWTotalPojo dormEWTotalPojo : dormEWTotalPojos) {
            dormId = dormEWTotalPojo.getDormId();
            waterAndEnergyList = hashMap.get(dormId);
            if (waterAndEnergyList == null || waterAndEnergyList.size() < 1) {
                waterAndEnergyList = new ArrayList<>();
                waterAndEnergyList.add(dormEWTotalPojo);
                hashMap.put(dormId, waterAndEnergyList);
            } else {
                waterAndEnergyList.add(dormEWTotalPojo);
                hashMap.put(dormId, waterAndEnergyList);
            }
        }
        //获取时间段内宿舍人员记录表
        List<DormManageToUserRecordsBean> userRecordsBeans = dormManageMapper.getDormManageToUserRecords(map);
        for (Map.Entry<String, List<DormEWTotalPojo>> entry : hashMap.entrySet()) {
            PeoplesFeeForDormPojo peoplesFeeForDormPojo = new PeoplesFeeForDormPojo();
            peoplesFeeForDormPojo.setDormId(entry.getKey());
            Double sumWater = 0.0;
            Double sumEnergy = 0.0;
            //汇总宿舍水电费
            for (DormEWTotalPojo pojo : entry.getValue()) {
                sumWater += pojo.getSumWater();
                sumEnergy += pojo.getSumEnergy();
            }
            List<UserRecordsPojo> userRecordsPojos = new ArrayList<>();
            //添加宿舍人员信息
            for (DormManageToUserRecordsBean userRecordsBean : userRecordsBeans) {
                if (entry.getKey().equals(userRecordsBean.getDormId())) {
                    UserRecordsPojo userRecordsPojo = new UserRecordsPojo();
                    userRecordsPojo.setCreateTime(userRecordsBean.getCreateTime());
                    userRecordsPojo.setRemoveTime(userRecordsBean.getRemoveTime());
                    userRecordsPojo.setUserId(userRecordsBean.getUserId());
                    userRecordsPojo.setUserName(userRecordsBean.getUserName());
                    userRecordsPojos.add(userRecordsPojo);
                }
            }
            peoplesFeeForDormPojo.setWaterFee(sumWater);
            peoplesFeeForDormPojo.setEnergyFee(sumEnergy);
            peoplesFeeForDormPojo.setUserRecordsPojos(userRecordsPojos);
            //当宿舍人员不为空时,不将宿舍人员添加到集合中
            if (peoplesFeeForDormPojo.getUserRecordsPojos() != null && peoplesFeeForDormPojo.getUserRecordsPojos().size() > 0) {
                peoplesFeeForDormPojos.add(peoplesFeeForDormPojo);
            }
        }
        return peoplesFeeForDormPojos;
    }

    /**
     * 个人用电统计
     * @param startTime
     * @param endTime
     * @param company
     * @return
     */
    public List<PersonOfDormStatisticsOut> getPersonOfDormStatistics(String startTime, String endTime, String company) {
        List<PersonOfDormStatisticsOut> personOfDormStatistics = dormManageMapper.getPersonOfDormStatistics(startTime, endTime, company);
        return personOfDormStatistics;
    }

    /**
     * 宿舍用电统计
     * @param startTime
     * @param endTime
     * @param company
     * @return
     */
    public List<DormStatisticsOut> getDormStatistics(String startTime, String endTime, String company) {
        List<DormStatisticsOut> dormStatistics = dormManageMapper.getDormStatistics(startTime, endTime, company);
        return dormStatistics;
    }


}
